import axios from "axios";
import type {
  AxiosInstance,
  AxiosResponse,
  InternalAxiosRequestConfig,
} from "axios";

// 创建axios实例
const apiClient: AxiosInstance = axios.create({
  baseURL: "/api", // 添加基础URL以便使用代理
  timeout: 10000, // 请求超时时间
  headers: {
    "Content-Type": "application/json",
  },
});

// 请求拦截器
apiClient.interceptors.request.use(
  (config: InternalAxiosRequestConfig) => {
    // 在发送请求之前做些什么
    // 例如添加认证token
    // if (token) {
    //   config.headers.Authorization = `Bearer ${token}`;
    // }
    return config;
  },
  (error) => {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);

// 响应拦截器
apiClient.interceptors.response.use(
  (response: AxiosResponse) => {
    // 对响应数据做点什么
    return response.data;
  },
  (error) => {
    // 对响应错误做点什么
    if (error.response?.status === 401) {
      // 处理未授权错误
      console.error("Unauthorized access");
    }
    return Promise.reject(error);
  }
);

export default apiClient;

// 定义API响应类型
interface ApiResponse<T> {
  code: number;
  data: T;
  message: string;
  taskId?: string; // 添加taskId字段
}

// 抖音视频数据响应类型
interface DouyinVideosResponse {
  list: any[];
}

// 七牛云上传token响应类型
// interface UploadTokenResponse {
//   token: string;
//   domain: string;
// }

// 更新为正确的类型定义
type QiniuUploadTokenResponse = string;

// 示例API调用函数
export const apiService = {
  // 获取数据示例
  getData: (id: string) => {
    return apiClient.get<ApiResponse<any>>(`/data/${id}`);
  },

  // 创建数据示例
  createData: (data: any) => {
    return apiClient.post<ApiResponse<any>>("/data", data);
  },

  // 更新数据示例
  updateData: (id: string, data: any) => {
    return apiClient.put<ApiResponse<any>>(`/data/${id}`, data);
  },

  // 删除数据示例
  deleteData: (id: string) => {
    return apiClient.delete<ApiResponse<any>>(`/data/${id}`);
  },

  // 获取抖音视频数据
  getDouyinVideos: () => {
    return apiClient.get<ApiResponse<DouyinVideosResponse>>(
      "/v1/admin/douyin/videos"
    );
  },

  // 获取个人账号数据
  getUserAccountData: (data: any) => {
    return apiClient.post<ApiResponse<any>>("/v1/command", data);
  },

  // 获取抖音个人账号视频数据
  getDouyinAccountVideos: (params?: any) => {
    const requestData = {
      platform: "douyin",
      command: "getVideosInfo",
      params: params || {},
    };

    return apiClient.post<ApiResponse<any>>("/v1/command", requestData);
  },

  // 获取抖音视频播放地址
  getDouyinVideoUrl: (videoId: string, accessToken: string) => {
    return apiClient.get<ApiResponse<any>>(
      `/v1/data/video/url/v1/?video_id=${videoId}&access_token=${accessToken}`
    );
  },

  // 获取七牛云上传token
  getQiniuUploadToken: () => {
    return apiClient.get<ApiResponse<QiniuUploadTokenResponse>>(
      "/v1/admin/qiniu/uploadToken"
    );
  },

  // 上传视频到抖音
  postVideoToDouyin: (params: {
    title: string;
    video: string;
    cover: string;
    talk: string[];
    summary: string;
    post_id: string;
  }) => {
    const requestData = {
      command: "postVideo",
      platform: "douyin",
      params: params,
    };

    return apiClient.post<ApiResponse<any>>("/v1/command", requestData);
  },
};
